﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AJWPFAdmin.Core.Components.AJTable.ViewModels
{
    public class AJTableSearchFormSchema : BindableBase
    {
        private string _label;
        /// <summary>
        /// 表单标签
        /// </summary>
        public string Label
        {
            get { return _label; }
            set { SetProperty(ref _label, value); }
        }

        private string[] _labels;
        /// <summary>
        /// 表单标签,日期范围选择使用
        /// </summary>
        public string[] Labels
        {
            get { return _labels; }
            set { SetProperty(ref _labels, value); }
        }

        private string _field;
        /// <summary>
        /// 表单字段名称
        /// </summary>
        public string Field
        {
            get { return _field; }
            set { SetProperty(ref _field, value); }
        }

        private object _value;
        /// <summary>
        /// 表单值
        /// </summary>
        public object Value
        {
            get { return _value; }
            set { SetProperty(ref _value, value); }
        }

        private string _comboxText;
        /// <summary>
        /// 下拉框绑定的输入字符串数据
        /// </summary>
        public string ComboxText
        {
            get { return _comboxText; }
            set { SetProperty(ref _comboxText, value); }
        }

        private Visibility _visible;
        public Visibility Visible
        {
            get { return _visible; }
            set { SetProperty(ref _visible, value); }
        }

        private ObservableCollection<AJTableFormSchemaItemOptions> _options;
        /// <summary>
        /// 表单选项, 针对 Select,Checkbox,RadioBox 有效
        /// </summary>
        public ObservableCollection<AJTableFormSchemaItemOptions> Options
        {
            get { return _options; }
            set { SetProperty(ref _options, value); }
        }

        private AJTableSchemaType _type;
        /// <summary>
        /// 表单类型
        /// </summary>
        public AJTableSchemaType Type
        {
            get { return _type; }
            set { SetProperty(ref _type, value); }
        }

        private bool _isPopular;
        /// <summary>
        /// 是否常用, 常用会直接显示, 否则会隐藏到高级筛选中
        /// </summary>
        public bool IsPopular
        {
            get { return _isPopular; }
            set { SetProperty(ref _isPopular, value); }
        }

        private DelegateCommand<SelectionChangedEventArgs> _comboxSelectionChangedCmd;
        public DelegateCommand<SelectionChangedEventArgs> ComboxSelectionChangedCmd =>
            _comboxSelectionChangedCmd ?? (_comboxSelectionChangedCmd = new DelegateCommand<SelectionChangedEventArgs>(ExecuteComboxSelectionChangedCmd));

        void ExecuteComboxSelectionChangedCmd(SelectionChangedEventArgs parameter)
        {
            if (parameter.AddedItems?.Count > 0)
            {
                parameter.AddedItems.Clear();
                var cbx = (ComboBox)parameter.Source;
                cbx.SelectedItem = null;
                cbx.SelectedValue = null;
            }
        }

        private DelegateCommand<RoutedEventArgs> _checkBoxCheckedChangedCmd;
        public DelegateCommand<RoutedEventArgs> CheckBoxCheckedChangedCmd =>
            _checkBoxCheckedChangedCmd ?? (_checkBoxCheckedChangedCmd = new DelegateCommand<RoutedEventArgs>(ExecuteCheckBoxCheckedChangedCmd));

        void ExecuteCheckBoxCheckedChangedCmd(RoutedEventArgs parameter)
        {
            parameter.Handled = true;
            var checkbox = parameter.Source as CheckBox;
            if (Type == AJTableSchemaType.MultiSelect)
            {
                if (Value == null)
                {
                    Value = new ObservableCollection<AJTableFormSchemaItemOptions>();
                }

                var option = checkbox.DataContext as AJTableFormSchemaItemOptions;

                var options = Value as ObservableCollection<AJTableFormSchemaItemOptions>;
                options.Clear();
                if (option.Checked)
                {
                    options.Add(option);
                }

                ComboxText = options.Count > 3
                ? $"{string.Join(";", options.Take(3).Select(p => p.Label))};+{options.Count - 3}"
                : string.Join(";", options.Select(p => p.Label));

            }
        }

        public object GetFormValue()
        {
            switch (_type)
            {
                case AJTableSchemaType.Input:

                case AJTableSchemaType.DateTimePicker:

                case AJTableSchemaType.RangePicker:

                case AJTableSchemaType.CheckBox:

                case AJTableSchemaType.RadioBox:

                case AJTableSchemaType.Switch:
                    return _value;
                case AJTableSchemaType.Select:
                    return (_value as AJTableFormSchemaItemOptions)?.Value ?? _comboxText;
                default:
                    return _value;
            }
        }

        public AJTableSearchFormSchema()
        {
            Visible = Visibility.Visible;
        }

    }
}
